var fs = require("fs");
var express = require("express");
var server = express();
var url = require("url")
var mysql = require("mysql");
var expressStatic = require("express-static");
var port = 81;
var db = mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"admin",
	database:"targeted_poverty_alleviation"

})
db.connect();
//解决跨域
// server.all('*', function(req, res, next) {  
//     res.header("Access-Control-Allow-Origin", "*");  
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");  
//     res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
//     res.header("X-Powered-By",' 3.2.1')  
//     res.header("Content-Type", "application/json;charset=utf-8");  
//     next();  
// });  
//注册用户
server.get("/addData",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	accountNumber = parse.accountNumber;
	var passWord = parse.passWord;
	console.log(accountNumber)
	console.log(passWord)

	if(accountNumber && passWord){
		db.query(`select * from personal_information where Accountnumber="${accountNumber}"`,(error,data)=>{
			if(data.length){
				console.log(data)

				console.log("此账号已注册！")
				response.end("error");
			}
			else{
				db.query(`INSERT INTO personal_information (Accountnumber,Password) VALUES ("${accountNumber}","${passWord}")`,(error,data)=>{
					if (error) {
						console.log(`注册失败，${accountNumber}注册失败，请求接口`+ port);
						response.end("error")
					}
					else{
						console.log(`注册成功，${accountNumber}注册成功，请求接口` + port);
						response.end("success")
					}
				})

			}
		})
	}
	else{
		console.log("账号不能为空");
		response.end("注册失败")
	}
	accountNumber1 = accountNumber;
})
//登录
server.get("/sigin",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var accountNumber = parse.accountNumber;
	AccountnumberO = accountNumber;
	var passWord = parse.passWord;
	if(accountNumber && passWord){
		db.query(`select * from personal_information where Accountnumber="${AccountnumberO}"`,(error,data)=>{
			if(data.length){
				if(data[0].Password == passWord){
					console.log("登录成功")
					response.end("1")
				}
				else{
					response.end("error")
				}
			}
			else{
				response.end("error")
			}
		})
	}
	else{
		console.log("请输入账号或密码");
		response.end("error")
	}
})
//管理员登录

server.get("/admin",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	accountNumber1 = parse.accountNumber;
	var passWord = parse.passWord;
	if(accountNumber1 && passWord){
		db.query(`select * from admin where Accountnumber="${accountNumber1}"`,(error,data)=>{
			if(data.length){
				if(data[0].Password == passWord){
					console.log("登录成功")
					response.end("1")
				}
				else{
					response.end("error")
				}
			}
			else{
				response.end("error")
			}
		})
	}
	else{
		console.log("请输入账号或密码");
		response.end("error")
	}
})
//获取个人账号
//管理员
server.get("/Accountnumber",(request,response)=>{
	db.query(`SELECT * FROM admin WHERE Accountnumber = "${accountNumber1}"`,(error,data)=>{
		if(error){
			console.log("查询账号失败")
			response.end("error")

		}
		else{
			console.log("查询账号成功")
			response.end(JSON.stringify(data))
			console.log(data)
		}
	})
})
//已注册登录
server.get("/Accountnumber1",(request,response)=>{
	db.query(`SELECT * FROM personal_information WHERE Accountnumber = "${AccountnumberO}"`,(error,data)=>{
		if(error){
			console.log("查询账号失败")
			response.end("error")

		}
		else{
			console.log("查询账号成功")
			response.end(JSON.stringify(data))
			console.log(data)
		}
	})
})

//获取正在注册个人信息
server.get("/users",(request,response)=>{
	db.query(`SELECT * FROM personal_information WHERE Accountnumber = "${accountNumber}"`,(error,data)=>{
		if(error){
			console.log("查询登录账号信息失败")
			response.end("error")

		}
		else{
			console.log("查询登录账号信息成功")
			response.end(JSON.stringify(data))
			console.log(data)
		}
	})
})
//获取已注册个人信息
server.get("/users1",(request,response)=>{
	db.query(`SELECT * FROM personal_information WHERE Accountnumber = "${AccountnumberO}"`,(error,data)=>{
		if(error){
			console.log("查询登录账号信息失败")
			response.end("error")

		}
		else{
			console.log("查询登录账号信息成功")
			response.end(JSON.stringify(data))
			console.log(data)
		}
	})
})
//注册添加个人基本信息
server.get("/appendData",function(request,response){
	var parse = url.parse(request.url,true).query;
	var IDnumber = parse.IDnumber;
	var Name = parse.Name;
	var Sex = parse.Sex;
	var Telephone = parse.Telephone;
	var Document_type =parse.Document_type;
	var Accountnumber = parse.Accountnumber;
	var Password = parse.Password;
	var Password1 = parse.Password1;
	var Email = parse.Email;

	if (IDnumber && Name && Sex && Telephone && Document_type && Password && Password1 && Email) {

		if(Password == Password1){
			db.query(`UPDATE personal_information SET IDnumber='${IDnumber}',Name='${Name}',Sex='${Sex}',Telephone='${Telephone}',Document_type='${Document_type}',Password1='${Password1}',Password='${Password}',Email='${Email}' WHERE Accountnumber = '${Accountnumber}'`,(error,data)=>{
				console.log(error)
				if (error) {
					console.log(`修改"${Name}"信息失败`)
					response.end("error")
				}
				else{
					console.log(`修改"${Name}"信息成功`)
					response.end("success")
				}
			})
		}
		else{
			console.log("请检查密码")
			response.end("error")
		}
	}
	else{
		console.log(`修改"${Name}"信息失败!`)
		response.end("error")
	}
})

//修改数据
server.get("/changeData",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var IDnumber = parse.IDnumber;
	var Name = parse.Name;
	var Sex = parse.Sex;
	var Telephone = parse.Telephone;
	var Document_type =parse.Document_type;
	var Accountnumber = parse.Accountnumber;
	var Password = parse.Password;
	var Password1 = parse.Password1;
	var Email = parse.Email;
	
	db.query(`UPDATE personal_information SET IDnumber='${IDnumber}',Name='${Name}',Sex='${Sex}',Telephone='${Telephone}',Document_type='${Document_type}',Password1='${Password1}',Password='${Password}',Email='${Email}' WHERE Accountnumber = '${Accountnumber}'`,(error,data)=>{
		if(error){
			response.end("error");
			console.log("error");
		}
		else{
			response.end("success");
			console.log("success")
			console.log(JSON.stringify(data))
		}
	})
})
server.get("/deleteData",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	
	var Accountnumber = parse.Accountnumber;
	
	db.query(`DELETE FROM personal_information WHERE Accountnumber = '${Accountnumber}'`,(error,data)=>{
		if(error){
			response.end("error");
			console.log("error");
		}
		else{
			response.end("success");
			console.log("success")
			console.log(JSON.stringify(data))
		}
	})
})
server.get("/add",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var Name = parse.Name;
	var Sex = parse.Sex;
	var Telephone = parse.Telephone;
	var Accountnumber = parse.Accountnumber;
	var Password = parse.Password;
	var Password1 = parse.Password1;
	var Document_type =parse.Document_type;
	var IDnumber = parse.IDnumber;
	var Email = parse.Email;
	console.log(Accountnumber)
	console.log(parse)
	if (IDnumber && Name && Sex && Telephone && Accountnumber && Document_type && Password && Password1 && Email) {
		db.query(`select * from personal_information where Accountnumber="${Accountnumber}"`,(error,data)=>{
			if(data.length){
				console.log(data)

				console.log("此账号已注册！")
				response.end("error");
			}
			else{
				db.query(`INSERT INTO personal_information (IDnumber,Name,Sex,Telephone,Document_type,Accountnumber,Password1,Password,Email) VALUES ('${IDnumber}','${Name}','${Sex}','${Telephone}','${Document_type}','${Accountnumber}','${Password1}','${Password}','${Email}')`,(error,data)=>{

					if (error) {
						console.log(error);
						response.end("error")
					}
					else{
						console.log(`插入成功，${Accountnumber}插入成功，请求接口` + port);
						response.end("success")
					}
				})

			}
		})
	}
	else{
		console.log("账号不能为空");
		response.end("error")
	}
})
//管理员获取所有用户信息
server.get("/information",(requset,response)=>{
	db.query(`SELECT * FROM personal_information`,(error,data)=>{
		if(error){
			console.log("获取取所有人信息失败")
			response.end("error")
		}
		else{
			console.log("获取取所有人信息成功")
			console.log(JSON.stringify(data))
			response.end(JSON.stringify(data))
		}
	})
})
// 管理员搜索
server.get("/search",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var infor = parse.message;
	db.query(`SELECT * FROM personal_information WHERE Name="${infor}"`,(error,data)=>{
		if(data.length){
			if(error){
				console.log("获取取当前信息失败")
				response.end("error")
			}
			else{
				console.log("获取取当前信息成功")
				console.log(JSON.stringify(data))
				response.end(JSON.stringify(data))
			}
		}
		else{
			console.log("未搜到该数据！")
			response.end("error")
		}
	})
})


//服务器重启
setInterval(function(){
	db.query(`SELECT * FROM personal_information`)
},720000)
server.use(expressStatic(__dirname + `/static`));
//监听端口；
server.listen(port)
console.log(`runing of ${port}`)